package cube.utils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

/**
 * SQL工具
 * 
 * @author abiao
 */
public class SQLUtil {

    /**
     * 设置SQL参数
     */
    public static void setSQLParams(PreparedStatement stmt, Object[] params) throws SQLException {
        if (params == null) {
            return;
        }
        for (int i = 0; i < params.length; i++) {
            Object p = params[i];
            int index = i + 1;
            if (p == null) {
                stmt.setNull(index, Types.NULL);
            } else if (p instanceof String) {
                stmt.setString(index, (String) p);
            } else if (p instanceof Integer) {
                stmt.setInt(index, (Integer) p);
            } else if (p instanceof Long) {
                stmt.setLong(index, (Long) p);
            } else if (p instanceof Date) {
                stmt.setString(index, TimeUtil.getSQLTime((Date) p));
            } else if (p instanceof byte[]) {
                stmt.setBytes(index, (byte[]) p);
            } else {
                throw new SQLException("Unsupport SQL Param Type!");
            }
        }
    }
}
